﻿function BookmarkViewModel(parent) {
    var self = this;

    self.ExpertLists = ko.observableArray([]);
    self.companyLists = ko.observableArray([]);
    self.positionLists = ko.observableArray([]);
    self.ExpertListNameExistsAlertVisible = ko.observable(false);
    self.companyListNameExistsAlertVisible = ko.observable(false);
    self.positionListNameExistsAlertVisible = ko.observable(false);
    self.newExpertList = ko.observable(new ExpertList({ }));
    self.newCompanyList = ko.observable(new CompanyList({ }));
    self.newPositionList = ko.observable(new PositionList({ }));
    self.bookmarkModel = ko.observable(new BookmarkModel({ }));
    self.profileAddedToExpertListAlert = ko.observable(false);
    self.companyAddedToCompanyListAlert = ko.observable(false);
    self.positionAddedToPositionListAlert = ko.observable(false);

    self.loadExpertLists = function() {
        $.getJSON("/ExpertList/List", function(data) {
            var expertLists = $.map(data, function(item) { return new ExpertList(item); });
            self.ExpertLists(expertLists);
        });
    };
    self.loadCompanyLists = function() {
        $.getJSON("/CompanyList/List", function(data) {
            var companyLists = $.map(data, function(item) { return new CompanyList(item); });
            self.companyLists(companyLists);
        });
    };
    self.loadPositionLists = function() {
        $.getJSON("/PositionList/List", function(data) {
            var positionLists = $.map(data, function(item) { return new positionList(item); });
            self.positionLists(positionLists);
        });
    };

    //SAVE Expert LIST
    self.saveExpertList = function() {
        $.ajax({
                url: "/ExpertList/Save",
                type: 'post',
                data: ko.toJSON(self.newExpertList()),
                contentType: 'application/json',
                success: function(result) {
                    if (result == "Ok") {
                        $('#CreateExpertListDialog').modal('hide');
                        self.ExpertListNameExistsAlertVisible(false);
                        self.loadExpertLists();
                    } else {
                        self.ExpertListNameExistsAlertVisible(true);
                    }
                }
            });
        self.newExpertList(new ExpertList({ }));
    };
    self.cancelExpertList = function() {
        $('#CreateExpertListDialog').modal('hide');
        self.ExpertListNameExistsAlertVisible(false);
        self.newExpertList(new ExpertList({ }));
    };

    //SAVE COMPANY LIST
    self.saveCompanyList = function() {
        $.ajax({
                url: "/CompanyList/Save",
                type: 'post',
                data: ko.toJSON(self.newCompanyList()),
                contentType: 'application/json',
                success: function(result) {
                    if (result == "Ok") {
                        $('#CreateCompanyListDialog').modal('hide');
                        self.companyListNameExistsAlertVisible(false);
                        self.loadCompanyLists();
                    } else {
                        self.companyListNameExistsAlertVisible(true);
                    }
                }
            });
        self.newCompanyList(new CompanyList({ }));
    };
    self.cancelCompanyList = function() {
        $('#CreateCompanyListDialog').modal('hide');
        self.companyListNameExistsAlertVisible(false);
        self.newCompanyList(new CompanyList({ }));
    };

    //SAVE position LIST
    self.savePositionList = function() {
        $.ajax({
                url: "/PositionList/Save",
                type: 'post',
                data: ko.toJSON(self.newPositionList()),
                contentType: 'application/json',
                success: function(result) {
                    if (result == "Ok") {
                        $('#CreatePositionListDialog').modal('hide');
                        self.positionListNameExistsAlertVisible(false);
                        self.loadPositionLists();
                    } else {
                        self.positionListNameExistsAlertVisible(true);
                    }
                }
            });
        self.newPositionList(new PositionList({ }));
    };
    self.cancelPositionList = function() {
        $('#CreatePositionListDialog').modal('hide');
        self.positionListNameExistsAlertVisible(false);
        self.newPositionList(new PositionList({ }));
    };

    //ADD PROFILE TO EXPERTLIST
    self.selectProfileLoadExpertLists = function(profile) {
        self.loadExpertLists();
        //reset bookmark model
        self.bookmarkModel(new BookmarkModel({}));
        self.bookmarkModel().ExpertProfile(profile);
    };
    self.addProfileToExpertList = function(expertList) {
        self.bookmarkModel().ExpertList(expertList);
        $.ajax({
                url: "/ExpertList/AddProfile",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                success: function() {
                    self.profileAddedToExpertListAlert(true);
                    setTimeout(function() { self.profileAddedToExpertListAlert(false); }, 5000);
                }
            });
    };

    //ADD COMPANY TO COMPANYLIST
    self.selectCompanyLoadCompanyLists = function(company) {
        self.loadCompanyLists();
        //reset bookmark model
        self.bookmarkModel(new BookmarkModel({}));
        self.bookmarkModel().CompanyProfile(company);
    };
    self.addCompanyToCompanyList = function(companyList) {
        self.bookmarkModel().CompanyList(companyList);
        $.ajax({
                url: "/CompanyList/AddCompany",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                success: function() {
                    self.companyAddedToCompanyListAlert(true);
                    setTimeout(function() { self.companyAddedToCompanyListAlert(false); }, 5000);
                }
            });
        };
        //ADD Position TO PositionLIST
        self.selectPositionLoadPositionLists = function (position) {
            self.loadPositionLists();
            //reset bookmark model
            self.bookmarkModel(new BookmarkModel({ }));
            self.bookmarkModel().Position(position);
        };
        self.addPositionToPositionList = function (positionList) {
            self.bookmarkModel().PositionList(positionList);
            $.ajax({
                url: "/PositionList/AddPosition",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                success: function () {
                    self.positionAddedToPositionListAlert(true);
                    setTimeout(function () { self.positionAddedToPositionListAlert(false); }, 5000);
                }
            });
        };
};